package forumSystem;

import java.nio.ByteBuffer;
import java.util.Vector;

import protocol.UsersConections;

import msgParser.Strings;

import reactor.ConnectionHandler;
import tokenizer.MessageTokenizer;
import tokenizer.StringMessage;

public class Notification implements Runnable
	{
		private String message;
		private Vector<Integer> toNotify;
		
		public Notification(String msg, Vector<Integer> users) 
		{
			message = msg;
			toNotify = users;
		}
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		public void run()
		{
			int userId;
			
			StringMessage toSend = createMessage( Strings.NOTIFY, message);
			for (int i = 0; i < toNotify.size(); i++)
			{			
				userId = toNotify.elementAt(i);
				System.out.println("this is the sizeeeeeeeeeeeee: "+ toNotify.size());
				ConnectionHandler handler = UsersConections.getHandler(userId);
				try
				{
					MessageTokenizer tokenizer = handler.getTokenizer();
					ByteBuffer buf = tokenizer.getBytesForMessage(toSend);
					handler.addOutData(buf);
					System.out.println(message + "***********");
					handler.write();
				}
				catch(Exception e)
				{
					System.out.println("gotcha!!!!!!!!!!!!!!!!!!!!!!!!!");
				}
			}
		}
		
		public StringMessage createMessage(String header, String msg)
		{
			return new StringMessage(header + Strings.DELIMITER + msg);
		}
	}